package com.usian.user.controller;

import com.usian.common.vo.ResultVo;
import com.usian.user.pojo.User;
import com.usian.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 06 26 08 45
 **/
@RestController
@RequestMapping("/api/user")
@Slf4j
@CrossOrigin
public class UserController {
   @Autowired
   UserService userService;


   @RequestMapping("/findAll")
   public List<User> findAll() {
       return userService.findAllUser();
   }


   @RequestMapping("/findUserById")
   public User findUserById(Integer id) {
       return userService.findUserById(id);
   }

    /**
     * {id}: 占位符
     * @param id
     * @return
     */
   @RequestMapping("/getUserById/{id}")
   public User getUserById(@PathVariable Integer id) {
       log.info("8072短口号正在提供服务--------：{}",id);
       return userService.findUserById(id);
   }


   @PostMapping("/login")
   public ResultVo login(@RequestBody User user){
       log.info("正在登录----------");
       User loginUser = userService.login(user);
       if(null==loginUser){
           return ResultVo.error();
       }else{
           return ResultVo.ok();
       }



   }


   @PostMapping("/register")
   public ResultVo register(@RequestBody User user){
       log.info("=====register=======");
       int i = userService.save(user);
       if(i>0){
           return ResultVo.ok();
       }else {
           return ResultVo.error();
       }
   }
}
